﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.OA.MsNotify;
using Newtonsoft.Json;
using System.Collections.Generic;
using Ccit.OA.Utils;
using Ccit.Utils;
namespace Ccit.OA.Notify
{
    public partial class notify_manage_queryedit : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _hidsQueryId.Value = (null != Request.QueryString["QueryId"]) ? Request.QueryString["QueryId"].ToString() : "";
                _hidTypeId.Value = (null != Request.QueryString["Type"]) ? Request.QueryString["Type"].ToString() : ""; 
            }
            DataBinde();
            if (_hidsQueryId.Value != "")
                InitPage();
            btnSearch.Attributes["onclick"] = "javascript:document.getElementById('_hidXML').value=doSelect();";
        }

        private void DataBinde()
        {
            CtrInterface.BindDropDown(ref ddlImportance, "bas_a1", "bas_a1_30", "bas_a1_40", true, "全部", "bas_a1_20='995'");
            NotifyBLL Bll = new NotifyBLL();
            DataSet dSet = new DataSet();           
            dSet = Bll.GetDictLibMes();             
            if (dSet.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dSet.Tables[0].Rows.Count; i++)
                {
                    DataRow dRow = dSet.Tables[0].Rows[i];
                    lstType.Items.Add(new ListItem(dRow["typeName"].ToString(), dRow["typeNum"].ToString()));
                }
            }
            ddlPubFlag.Items.Add(new ListItem("全部",""));
            if (_hidTypeId.Value == "1")
            {
                lblstate.Text = "发布标志";
                ddlPubFlag.Items.Add(new ListItem ("已发布","0"));
                ddlPubFlag.Items.Add(new ListItem("未发布","1"));
            }
            else
            {
                lblstate.Text = "阅读标志";
                ddlPubFlag.Items.Add(new ListItem("未读", "1"));
                ddlPubFlag.Items.Add(new ListItem("已读", "4"));
                ddlPubFlag.Items.Add(new ListItem("已读未回", "2"));
                ddlPubFlag.Items.Add(new ListItem("已读已回", "3"));
            }
        }

        private void InitPage()
        {
            NotifyBLL Bll = new NotifyBLL();
            MsQueryClass ms = Bll.GetQueryEdit(_hidsQueryId.Value);
            if(ms!=null)
            {
                string[] content = ms.content.Split('~');
                txtTitle.Text = (content[0].ToString()!="")?content[0].ToString():"";
                txtContent.Text = (content[1].ToString() != "") ? content[1].ToString() : "";
                _hidUserId.Value = (content[2].ToString() != "") ? content[2].ToString() : "";
                if (_hidUserId.Value != "")
                    txtUserName.Text = SysInterface.GetNameStrByIdStr(_hidUserId.Value, NameType.USER, ""); 
                ddlImportance.SelectedValue = (content[3].ToString() != "") ? content[3].ToString() : "";
                ddlPubFlag.SelectedValue = (content[4].ToString() != "") ? content[4].ToString() : "";
                //lstType.SelectedValue = (content[5].ToString() != "") ? content[5].ToString() : "";
                string type = (content[6].ToString() != "") ? content[6].ToString() : "";
                if(type=="0")
                { radiotype.Checked = true; }
                if (type == "1")
                { radioDay.Checked = true; }
                if (type == "2")
                { radioWeek.Checked = true; }
                if (type == "3")
                { radioMonth.Checked = true; } 
                if (type == "4")
                {
                    radiorange.Checked = true;
                    txtrange.Text = (content[7].ToString() != "") ? content[7].ToString():"";
                }
                if (type == "5")
                {
                    radioDesignated.Checked = true;
                    txtBegInTime.Text = (content[7].ToString() != "") ? content[7].ToString() : "";
                    txtEndTime.Text = (content[8].ToString() != "") ? content[8].ToString() : "";
                }
            }
        }
        [global::System.Web.Services.WebMethod]
        public static string DoSaveMes(string strJson,string type)
        {
            NotifyBLL Bll = new NotifyBLL();
            MsQueryClass ms = (MsQueryClass)JavaScriptConvert.DeserializeObject(strJson, typeof(MsQueryClass));
            List<string> lstResult = Bll.DoSaveQuery(ms);
            var newObj = new { result = lstResult[0], message = lstResult[1], ctype = type };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            Bll = null;
            newObj = null;
            return serExpr;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Session["QryClause"] = _hidXml.Value;
            string pageSource = _hidTypeId.Value;
            string url = ""; 
            if (pageSource == "1")   //信息发布
            {
                url = "notify_manage_list.aspx";
            }
            if (pageSource == "2")   //信息查询
            {
                url = "notify_view_list.aspx" ;
            }          
            Response.Write("<script>parent.document.location='" + url + "'</script>");
        }       
    }
}
